﻿using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using MaterialDesignColors;
using MaterialDesignThemes.Wpf;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Autodesk.Revit.DB.Architecture;
using ZhuXingFamilys.ModelInformation.Entity.Engineering;
using DataGridTextColumn = System.Windows.Controls.DataGridTextColumn;

namespace ZhuXingFamilys.ModelInformation
{
    /// <summary>
    /// GeneralDrawingInfo.xaml 的交互逻辑
    /// </summary>
    public partial class GeneralDrawingInfo : Window
    {
        private static GeneralDrawingInfo instance = null;
        private Document doc;
        public GeneralDrawingInfo(UIApplication uiapp)
        {
            this.doc = uiapp.ActiveUIDocument.Document;
            //ui库使用
            ColorZoneAssist.SetMode(new GroupBox(), ColorZoneMode.Light);
            Hue hue = new Hue("name", System.Windows.Media.Color.FromArgb(1, 2, 3, 4), System.Windows.Media.Color.FromArgb(1, 5, 6, 7));
            InitializeComponent();
        }
        public static GeneralDrawingInfo GetInstance(UIApplication uiapp)
        {
            if (instance == null)
            {
                instance = new GeneralDrawingInfo(uiapp);
                return instance;
            }
            return instance;
        }
        private void Window_Closed(object sender, EventArgs e)
        {
            instance = null;
        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            // 小市政分类列表
            municipalList.ItemsSource = getMunicipalInfo();
        }

        private void municipalList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            municipalInfoView.ItemsSource = null;
            municipalInfoView.Columns.Clear();
            var selectedValue = municipalList.SelectedValue.ToString();

            if ("井".Equals(selectedValue))
            {
                generateTable<well>(municipalInfoView);
                municipalInfoView.ItemsSource = getWellInfo();
            }
            if ("道路".Equals(selectedValue))
            {
                generateTable<road>(municipalInfoView);
                municipalInfoView.ItemsSource = getRoadInfo();
            }
            if ("出入口".Equals(selectedValue))
            {
                generateTable<gate>(municipalInfoView);
                municipalInfoView.ItemsSource = getGateInfo();
            }
            if ("停车位".Equals(selectedValue))
            {
                generateTable<parkingSpace>(municipalInfoView);
                // municipalInfoView.ItemsSource = getParkingInfo();
            }
            if ("室外场地".Equals(selectedValue))
            {
                generateTable<outdoorVenue>(municipalInfoView);
                municipalInfoView.ItemsSource = getOutdoorVenueInfo();
            }
            if ("绿地".Equals(selectedValue))
            {
                generateTable<greenLand>(municipalInfoView);
                municipalInfoView.ItemsSource = getGreenLandInfo();
            }
            if ("树木".Equals(selectedValue))
            {
                generateTable<trees>(municipalInfoView);

            }
            if ("场地地形".Equals(selectedValue))
            {
                generateTable<siteTopography>(municipalInfoView);
                municipalInfoView.ItemsSource = getSiteTopographyInfo();
            }
            if ("出入口构筑物".Equals(selectedValue))
            {
                generateTable<gateStructures>(municipalInfoView);

            }
            if ("城市家具".Equals(selectedValue))
            {
                generateTable<urbanFurniture>(municipalInfoView);

            }
            if ("IOT设备".Equals(selectedValue))
            {
                generateTable<IOT_equipment>(municipalInfoView);

            }
        }


        // 小市政分类列表
        private List<string> getMunicipalInfo()
        {
            List<string> sList = new List<string>()
            {
                "井","道路","出入口","停车位","室外场地","绿地","树木","场地地形","出入口构筑物","城市家具","IOT设备"
            };
            return sList;
        }

        // 根据实体类生成表格
        private void generateTable<T>(DataGrid dataGrid)
        {
            var columns = dataGrid.Columns;
            columns.Clear();
            // 序号列
            DataGridTextColumn xuhao = new DataGridTextColumn();
            xuhao.Header = "序号";
            xuhao.Binding = new System.Windows.Data.Binding()
            {
                Path = new PropertyPath("Header"),
                RelativeSource = new RelativeSource()
                {
                    AncestorType = typeof(DataGridRow)
                }
            };
            columns.Add(xuhao);

            // 反射实体类新建表格字段和绑定关系
            PropertyInfo[] peroperties = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
            foreach (PropertyInfo peroperty in peroperties)
            {
                if (peroperty.Name.Equals("Id")) continue;
                object[] objs = peroperty.GetCustomAttributes(typeof(DescriptionAttribute), true);
                if (objs.Length > 0)
                {
                    // MessageBox.Show(peroperty.Name + ((DescriptionAttribute)objs[0]).Description);
                    DataGridTextColumn dgtc = new DataGridTextColumn()
                    {
                        Header = ((DescriptionAttribute)objs[0]).Description,
                        Binding = new System.Windows.Data.Binding()
                        {
                            Path = new PropertyPath(peroperty.Name)
                        }
                    };
                    columns.Add(dgtc);
                }

            }
        }

        // 数据表格添加行号
        private void ExcelDataGrid_LoadingRow(object sender, DataGridRowEventArgs e)
        {
            e.Row.Header = (e.Row.GetIndex() + 1).ToString();
        }

        #region 信息获取
        // 井
        private List<well> getWellInfo()
        {
            List<well> wells = new List<well>();
            var collector = new FilteredElementCollector(doc);
            collector.OfClass(typeof(FamilyInstance));
            foreach (Element elem in collector.ToElements())
            {
                string family = elem.get_Parameter(BuiltInParameter.ELEM_FAMILY_PARAM).AsValueString();
                if (!string.IsNullOrEmpty(family) && family.Contains("井"))
                {
                    well well = new well();
                    well.Id = elem.Id.ToString();

                    if (elem.LookupParameter("井编号") != null)
                        well.code = elem.LookupParameter("井编号").AsString();
                    
                    Parameter name = elem.get_Parameter(BuiltInParameter.ELEM_FAMILY_PARAM);
                    if (name != null)
                        well.name = name.AsValueString();
                    
                    Parameter type = elem.get_Parameter(BuiltInParameter.ELEM_TYPE_PARAM);
                    if (type != null)
                        well.type = type.AsValueString();
                    
                    if (elem.LookupParameter("专业类别") != null)
                        well.professional_type = elem.LookupParameter("专业类别").AsString();
                    
                    Parameter eleSysType = elem.get_Parameter(BuiltInParameter.RBS_PIPING_SYSTEM_TYPE_PARAM);
                    if (eleSysType != null)
                    {
                        well.system_type = eleSysType.AsValueString();
                    }
                    
                    if (elem.LookupParameter("二级系统类型") != null)
                        well.secondary_system_type = elem.LookupParameter("二级系统类型").AsString();
                    
                    if (elem.LookupParameter("是否为过渡段") != null)
                        well.id_transition_section = elem.LookupParameter("是否为过渡段").AsValueString();

                    wells.Add(well);
                }
            }
            return wells;
        }
        // 停车位信息
        private List<parkingSpace> getParkingInfo()
        {

            return null;
        }
        // 出入口信息
        private List<gate> getGateInfo()
        {
            var collector = new FilteredElementCollector(doc);
            collector.OfCategory(BuiltInCategory.OST_Site).OfClass(typeof(FamilyInstance));
            List<gate> gates = new List<gate>();
            foreach (Element elem in collector.ToElements())
            {
                if (elem.Name != null && elem.Name.Contains("机动车出入口"))
                {
                    gate g = new gate();
                    g.Id = elem.Id.ToString();
                    Parameter name = elem.get_Parameter(BuiltInParameter.ELEM_FAMILY_PARAM);
                    if (name != null)
                        g.name = name.AsValueString();
                    Parameter type = elem.get_Parameter(BuiltInParameter.ELEM_TYPE_PARAM);
                    if (type != null)
                        g.type = type.AsValueString();
                    Parameter w = elem.LookupParameter("出入口宽度");
                    if (w != null)
                        g.width = Math.Round(UnitUtils.ConvertFromInternalUnits(w.AsDouble(), w.DisplayUnitType), 3);
                    if (elem.LookupParameter("出入口位置 X") != null)
                        g.positionX = elem.LookupParameter("出入口位置 X").AsValueString();
                    if (elem.LookupParameter("出入口位置 Y") != null)
                        g.positionY = elem.LookupParameter("出入口位置 Y").AsValueString();
                    if (elem.LookupParameter("出入口方向") != null)
                        g.direction = elem.LookupParameter("出入口方向").AsValueString();

                    if (elem.LookupParameter("是否是主出入口") != null)
                        g.is_main = elem.LookupParameter("是否是主出入口").AsValueString();
                    
                    if (elem.LookupParameter("是否为紧急出入口") != null)
                        g.is_emergency = elem.LookupParameter("是否为紧急出入口").AsValueString();

                    if (elem.LookupParameter("出入口高程") != null)
                        g.elevation = elem.LookupParameter("出入口高程").AsValueString();

                    gates.Add(g);
                }
            }
            return gates;
        }
        // 场地信息
        private List<outdoorVenue> getOutdoorVenueInfo()
        {
            List<outdoorVenue> outdoorVenues = new List<outdoorVenue>();

            var collector = new FilteredElementCollector(doc);
            collector.OfCategory(BuiltInCategory.OST_Site).OfClass(typeof(FamilyInstance));

            foreach (Element elem in collector.ToElements())
            {
                if (elem.Name != null && elem.Name.Contains("场地"))
                {
                    outdoorVenue og = new outdoorVenue();
                    og.Id = elem.Id.ToString();
                    Parameter name = elem.get_Parameter(BuiltInParameter.ELEM_FAMILY_PARAM);
                    if (name != null)
                        og.name = name.AsValueString();
                    Parameter type = elem.get_Parameter(BuiltInParameter.ELEM_TYPE_PARAM);
                    if (type != null)
                        og.type = type.AsValueString();
                    if (elem.LookupParameter("铺装") != null)
                        og.paving = elem.LookupParameter("铺装").AsString();

                    if (elem.LookupParameter("边界线") != null)
                        og.boundaryLine = elem.LookupParameter("边界线").AsValueString();

                    if (elem.LookupParameter("是否为消防登高场地") != null)
                        og.is_fireProtection = elem.LookupParameter("是否为消防登高场地").AsValueString();

                    if (elem.LookupParameter("是否为应急避险场地") != null)
                        og.is_emergency = elem.LookupParameter("是否为应急避险场地").AsValueString();

                    if (elem.LookupParameter("是否为实土绿地") != null)
                        og.is_solidSoil = elem.LookupParameter("是否为实土绿地").AsValueString();

                    outdoorVenues.Add(og);
                }
            }
            return outdoorVenues;
        }
        // 道路信息
        private IEnumerable getRoadInfo()
        {
            List<road> roads = new List<road>();
            var collector = new FilteredElementCollector(doc);
            collector.OfCategory(BuiltInCategory.OST_Site).OfClass(typeof(FamilyInstance));

            foreach (Element elem in collector.ToElements())
            {
                if (elem.Name != null && elem.Name.Contains("道路"))
                {
                    road road = new road();
                    road.Id = elem.Id.ToString();
                    if (elem.LookupParameter("道路名称") != null)
                        road.name = elem.LookupParameter("道路名称").AsString();
                    Parameter type = elem.get_Parameter(BuiltInParameter.ELEM_TYPE_PARAM);
                    if (type != null)
                        road.type = type.AsValueString();
                    if (elem.LookupParameter("道路宽度") != null)
                        road.width = elem.LookupParameter("道路宽度").AsString();
                    if (elem.LookupParameter("道路中心线") != null)
                        road.centerline = elem.LookupParameter("道路中心线").AsValueString();
                    if (elem.LookupParameter("是否为消防车道") != null)
                        road.is_fireLane = elem.LookupParameter("是否为消防车道").AsValueString();
                    roads.Add(road);
                }
            }
            return roads;
        }
        // 绿地信息
        private IEnumerable getGreenLandInfo()
        {
            List<greenLand> greenLands = new List<greenLand>();
            var collector = new FilteredElementCollector(doc);
            collector.OfCategory(BuiltInCategory.OST_Topography).OfClass(typeof(TopographySurface));
            foreach (Element elem in collector.ToElements())
            {
                greenLand gl = new greenLand();
                var elName = elem.get_Parameter(BuiltInParameter.ROOM_NAME);
                if (elName != null && "绿地".Equals(elName.AsString()))
                {
                    gl.Id = elem.Id.ToString();
                    gl.name = elName.AsString();
                    if (elem.LookupParameter("绿地轮廓线") != null)
                        gl.contourLine = elem.LookupParameter("绿地轮廓线").AsValueString();
                    if (elem.LookupParameter("是否为下凹绿地") != null)
                        gl.is_sunken = elem.LookupParameter("是否为下凹绿地").AsValueString();
                    if (elem.LookupParameter("是否为实土绿地") != null)
                        gl.is_solidSoil = elem.LookupParameter("是否为实土绿地").AsValueString();
                    if (elem.LookupParameter("是否为应急避险场地") != null)
                        gl.is_emergency = elem.LookupParameter("是否为应急避险场地").AsValueString();
                    greenLands.Add(gl);
                }
            }
            return greenLands;
        }
        // 场地地形
        private List<siteTopography> getSiteTopographyInfo()
        {
            List<siteTopography> sites = new List<siteTopography>();
            var collector = new FilteredElementCollector(doc);
            collector.OfCategory(BuiltInCategory.OST_Topography);
            foreach (Element elem in collector.ToElements())
            {
                siteTopography st = new siteTopography();
                st.Id = elem.Id.ToString();
                var elName = elem.get_Parameter(BuiltInParameter.ROOM_NAME);
                if (elName != null)
                    st.name = elName.AsString();
                var elType = elem.get_Parameter(BuiltInParameter.ELEM_CATEGORY_PARAM);
                if (elType != null)
                    st.type = elType.AsValueString();
                if (elem.LookupParameter("铺装") != null)
                    st.paving = elem.LookupParameter("铺装").AsString();
                if (elem.LookupParameter("是否是消防登高场地") != null)
                    st.is_fireProtection = elem.LookupParameter("是否是消防登高场地").AsValueString();
                if (elem.LookupParameter("是否为应急避险场地") != null)
                    st.is_emergency = elem.LookupParameter("是否为应急避险场地").AsValueString();
                if (elem.LookupParameter("是否为实土绿地") != null)
                    st.is_solidSoil = elem.LookupParameter("是否为实土绿地").AsValueString();

                sites.Add(st);
            }

            return sites;
        }
        #endregion
    }
}
